package com.greatmay.service.impl;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

import javax.jws.WebService;

import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.FileType;
import org.apache.commons.vfs2.VFS;
import org.springframework.stereotype.Service;

import com.greatmay.service.FileViewer;
import com.greatmay.util.ConfigHolder;


/**
 * Implementation of UserManager interface.
 *
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 */
@Service("fileViewer")
@WebService
public class FileViewerImpl implements FileViewer{
    @Override
    public String getHtml(com.greatmay.model.FileType fileType, String dir) {
        FileObject baseDir;
        if (dir.charAt(dir.length()-1) == '\\') {
            dir = dir.substring(0, dir.length()-1) + "/";
        } else if (dir.charAt(dir.length()-1) != '/') {
            dir += "/";
        }


        StringBuilder builder = new StringBuilder("<ul class=\"jqueryFileTree\" style=\"display: none;\">");
        FileSystemManager fsManager;
        try {
            dir = java.net.URLDecoder.decode(dir, "UTF-8");
            fsManager = VFS.getManager();

            if (com.greatmay.model.FileType.BOOKS == fileType) {
                baseDir = fsManager.resolveFile(ConfigHolder.getConfigHolder().getConfigString(ConfigHolder.BOOK_BASEDIR));
            } else if (com.greatmay.model.FileType.VIDEOS == fileType) {
                baseDir = fsManager.resolveFile(ConfigHolder.getConfigHolder().getConfigString(ConfigHolder.VIDEO_BASEDIR));
            } else if (com.greatmay.model.FileType.PICS == fileType) {
                baseDir = fsManager.resolveFile(ConfigHolder.getConfigHolder().getConfigString(ConfigHolder.PIC_BASEDIR));
            } else if (com.greatmay.model.FileType.OTHERS == fileType) {
                baseDir = fsManager.resolveFile(ConfigHolder.getConfigHolder().getConfigString(ConfigHolder.OTHERS_BASEDIR));
            } else if (com.greatmay.model.FileType.EXPRESSES == fileType) {
                baseDir = fsManager.resolveFile(ConfigHolder.getConfigHolder().getConfigString(ConfigHolder.EXPRESS_BASEDIR));
            } else if (com.greatmay.model.FileType.RECOMMENDATIONS == fileType) {
                baseDir = fsManager.resolveFile(ConfigHolder.getConfigHolder().getConfigString(ConfigHolder.RECOMMENDATION_BASEDIR));
            } else {
                throw new IllegalArgumentException("FileViewerImpl.getHtml don't take " + fileType + " as argument.");
            }
            //TODO throw exception if dir is not a sub dir/file of baseDir
            FileObject file;
            if ("/".equals(dir)) {
                file = baseDir;
            } else {
                file = fsManager.resolveFile(baseDir, dir);
            }
            builder.append("<li class=\"file ext_none\"><a href=\"#\" rel=\"\">取消</a></li>");
            for (FileObject subFile : file.getChildren()) {
                if (FileType.FOLDER.equals(subFile.getType())) {
                    String fileName = subFile.getName().getBaseName();
                    builder.append("\n");
                    builder.append("<li class=\"directory collapsed\"><a href=\"#\" rel=\"" + baseDir.getName().getRelativeName(subFile.getName()) + "/\">"
                            + fileName + "</a></li>");
                }
            }
            for (FileObject subFile : file.getChildren()) {
                if (FileType.FILE.equals(subFile.getType())) {
                    String fileName = subFile.getName().getBaseName();
                    int dotIndex = fileName.lastIndexOf('.');
                    String ext = dotIndex > 0 ? fileName.substring(dotIndex + 1) : "";
                    builder.append("\n");
                    builder.append("<li class=\"file ext_" + ext + "\"><a href=\"#\" rel=\"" + baseDir.getName().getRelativeName(subFile.getName()) + "\">"
                    + fileName + "</a></li>");
                }
            }
            builder.append("\n");
            builder.append("</ul>");
        }
        catch (FileSystemException e) {
            e.printStackTrace(System.out);
            throw new RuntimeException(e);
        }
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        // TODO Auto-generated method stub
        return builder.toString();
    }

    public static void main(String[] args) throws FileSystemException {
        FileObject baseDir;
        FileSystemManager fsManager;
        fsManager = VFS.getManager();
        baseDir = fsManager.resolveFile("ftp://damei:Damei,2013@61.135.203.244/flv");
        System.out.println(baseDir.exists());
        System.out.println(baseDir);
        System.out.println(Arrays.toString(baseDir.getChildren()));
    }
}
